﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Remote_Desktop_Administrator
{
    public partial class FolderForm : Form
    {
        private bool blnUpdateFolder = false;
        private ComboBox CboBox1 = new ComboBox();
        private TreeView tvm = new TreeView();

        //
        // Overview:
        // Default constructor
        //
        public FolderForm()
        {
            InitializeComponent();
        }

        //
        // Overview:
        // Closes form
        //
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //
        // Overview:
        // Saves folder to folder array & closes form
        //
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtFolderName.Text == "")
            {
                MessageBox.Show("You must enter a folder name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                string strFolderName;

                try
                {
                    strFolderName = txtFolderName.Text;
                    if (!blnUpdateFolder)
                    {
                        int intIndex = -1;
                        intIndex = Connection.FindFolderByName(txtFolderName.Text);
                        if (intIndex == -1)
                        {
                            Connection.SaveNewFolder(txtFolderName.Text);
                            MessageBox.Show("Folder added successfully.", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Connection.RefreshCboFolders(CboBox1);
                        }
                        else
                        {
                            MessageBox.Show("Folder exists - please choose another name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.Close();
                        }
                    }
                    else
                    {
                        Connection.RenameFolder(tvm.SelectedNode.Name, txtFolderName.Text, tvm);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error while adding/adjusting folder - " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    this.Close();
                }
            }
        }

        //
        // Overview:
        // Accepts reference for connection form combo box so we can update it later...
        //
        public void ReceiveCboBox(ComboBox cb1)
        {
            CboBox1 = cb1;
        }

        //
        // Overview:
        // Accepts reference for main form treeview so we can update it later...
        //
        public void ReceiveTreeView(TreeView Tree1)
        {
            tvm = Tree1;
        }

        // various properties for external manipulation...
        public bool prop_UpdateFolder
        {
            get { return blnUpdateFolder; }
            set { blnUpdateFolder = value; }
        }
    }
}
